package com.ytjj.qmyx.admin.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.admin.config.SupplyApiConfig;
import com.ytjj.qmyx.admin.mapper.AdminMapper;
import com.ytjj.qmyx.admin.mapper.ProductLabelMapper;
import com.ytjj.qmyx.admin.mapper.ProductProductLabelMapper;
import com.ytjj.qmyx.admin.model.*;
import com.ytjj.qmyx.admin.model.request.BatchProductLabelRequest;
import com.ytjj.qmyx.admin.service.ProductLabelService;
import com.ytjj.qmyx.mall.model.BankerStoreAddressResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class ProductLabelServiceImpl implements ProductLabelService {

    @Resource
    private ProductLabelMapper productLabelMapper;

    @Resource
    private ProductProductLabelMapper productProductLabelMapper;

    @Resource
    private AdminMapper adminMapper;
    @Resource
    private SupplyApiConfig supplyApiConfig;


    @Override
    public CommonResult select(String productLabelName, Integer pageNum, Integer pageSize) {
        Map<String, String> map = new HashMap<>();
        map.put("productLabelName", productLabelName);
        map.put("pageNum", String.valueOf(pageNum));
        map.put("pageSize", String.valueOf(pageSize));
        String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/label/productLabelList", map, supplyApiConfig.getHeadsMap());
        if (StringUtils.isEmpty(resultJson)) {
            throw new ApiException("获取商品标签异常");
        }
        CommonPage commonPage = JSONObject.parseObject(resultJson, CommonPage.class);
        List<ProductLabel> list = JSONArray.parseArray(commonPage.getList().toString(), ProductLabel.class);
        list.stream().forEach(item -> {
            Admin admin = adminMapper.selectByPrimaryKey(item.getAdminId());
            item.setAdminName(admin.getCompellation());
        });
        commonPage.setList(list);
        return CommonResult.success(commonPage);
    }

    @Override
    public CommonResult delete(Integer productLabelId) {
        Map<String, String> map = new HashMap<>();
        map.put("productLabelId", String.valueOf(productLabelId));
        String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/label/delete", map, supplyApiConfig.getHeadsMap());
        CommonResult commonResult = JSONObject.parseObject(resultJson, CommonResult.class);
        return commonResult;
    }

    @Override
    public CommonResult addAndUpdate(String productLabelName, Integer productLabelId, Integer adminId, String colour) {
        Map<String, String> map = new HashMap<>();
        if (null != productLabelId) {
            map.put("productLabelId", String.valueOf(productLabelId));
        }
        map.put("productLabelName", productLabelName);
        map.put("adminId", String.valueOf(adminId));
        map.put("colour", colour);
        String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/label/addAndUpdate", map, supplyApiConfig.getHeadsMap());
        CommonResult commonResult = JSONObject.parseObject(resultJson, CommonResult.class);
        return commonResult;
    }

    @Override
    public CommonResult selectAll() {
        String resultJson = HttpClientUtil.doGet(supplyApiConfig.getUrl() + "/label/selectAllLabel", null, supplyApiConfig.getHeadsMap());
        List<ProductLabel> productLabels = JSONObject.parseObject(resultJson, ArrayList.class);
        return CommonResult.success(productLabels);
    }

    @Override
    public CommonResult addProductLabel(List<Integer> productIds, List<Integer> productLabelIds, Integer adminId) {
        BatchProductLabelRequest batchProductLabelRequest = new BatchProductLabelRequest();
        batchProductLabelRequest.setProductIds(productIds);
        batchProductLabelRequest.setProductLabelIds(productLabelIds);
        batchProductLabelRequest.setAdminId(adminId);
        String json = JSON.toJSONString(batchProductLabelRequest);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/label/batchAddProductLabel", json, supplyApiConfig.getHeadsMap());
        return JSONObject.parseObject(resultJson, CommonResult.class);
    }

    @Override
    public CommonResult batchDeleteProductLabel(List<Integer> productIds) {
        BatchProductLabelRequest batchProductLabelRequest = new BatchProductLabelRequest();
        batchProductLabelRequest.setProductIds(productIds);
        String json = JSON.toJSONString(batchProductLabelRequest);
        String resultJson = HttpClientUtil.doPostJson(supplyApiConfig.getUrl() + "/label/batchDeleteProductLabel", json, supplyApiConfig.getHeadsMap());
        return JSONObject.parseObject(resultJson, CommonResult.class);
    }
}
